package io.adrainty.bolee.file.web;

import cn.hutool.core.io.IoUtil;
import io.adrainty.bolee.file.models.entity.FileEntity;
import io.adrainty.bolee.file.models.entity.FilePartEntity;
import io.adrainty.bolee.file.models.request.UploadMultipartFileDTO;
import io.adrainty.bolee.file.service.IFileService;
import io.adrainty.bolee.framework.commons.basic.ResponseCode;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.exception.AppException;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/17 下午11:44
 */

@Slf4j
@Tag(name = "文件上传接口")
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Resource
    private IFileService fileService;

    @PostMapping(value = "up-load")
    @Operation(summary = "上传文件-简单上传")
    public ResultDTO<FileEntity> upload(@RequestParam("file") MultipartFile file, FileEntity param) {
        try (InputStream is = file.getInputStream()) {
            UploadMultipartFileDTO multipartFileDTO = UploadMultipartFileDTO.builder()
                    .originalFilename(file.getOriginalFilename())
                    .fileByte(IoUtil.readBytes(is))
                    .build();
            FileEntity fileEntity = fileService.upload(multipartFileDTO, param);
            return ResultDTOUtil.success(fileEntity);
        } catch (IOException e) {
            log.error("文件读取异常", e);
            throw new AppException(ResponseCode.FILE_READ_ERROR);
        }
    }

    @PostMapping(value = "initiate-multipart-up-load")
    @Operation(summary = "文件分片上传-初始化")
    public ResultDTO<FileEntity> initiateMultipartUpload(@RequestBody FileEntity fileVO){
        //初始化上传Id
        FileEntity fileVOResult = fileService.initiateMultipartUpload(fileVO);
        return ResultDTOUtil.success(fileVOResult);
    }

    @PostMapping(value = "up-load-part")
    @Operation(summary = "文件分片上传-上传分片")
    public ResultDTO<String> uploadPart(@RequestParam("file") MultipartFile file, FilePartEntity filePartVO)throws IOException {
        //构建文件上次对象
        UploadMultipartFileDTO uploadMultipartFile = UploadMultipartFileDTO
                .builder()
                .originalFilename(file.getOriginalFilename())
                .fileByte(IOUtils.toByteArray(file.getInputStream()))
                .build();
        //上传分片返回partETagJson
        String partETagJson = fileService.uploadPart(uploadMultipartFile,filePartVO);
        return ResultDTOUtil.success(partETagJson);
    }

    @PostMapping(value = "complete-multipart-up-load")
    @Operation(summary = "文件分片上传-合并分片")
    public ResultDTO<String> completeMultipartUpload(@RequestBody FileEntity fileVO) {
        //问上传分片返回partETagJson
        String eTagJson = fileService.completeMultipartUpload(fileVO);
        return ResultDTOUtil.success(eTagJson);
    }

}
